﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Toenda.Rails.System;
using Toenda.Rails.DataContainer;

public partial class Controls_ctrl_retrievement : System.Web.UI.UserControl {
	private AccountProvider ap = new AccountProvider();
	private ErrorHandler errhand = new ErrorHandler();

	/// <summary>
	/// Page_Load
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e) {
		if(Request.QueryString.Count > 0) {
			// error handling
			if(Request.QueryString.Get("message") != null) {
				ret.Visible = false;

				ErrorHandler handler = new ErrorHandler();
				long errcode = Convert.ToInt64(Request.QueryString.Get("message"));

				if(errcode == 12) {
					handler.ErrorCode = handler.Parse(errcode);
					ErrorType err = handler.ErrorCode;

					if(err != ErrorType.NoError) {
						LiteralControl litError = new LiteralControl();
						litError.Text = "<div class=\"errorBox\">";
						litError.Text += "<span class=\"txtH3\">" + handler.ErrorDescription + "</span>";
						litError.Text += "<br /><br />";
						litError.Text += "<span class=\"txtNormal\">" + handler.ErrorMessage + "</span>";
						litError.Text += "</div>";
						litError.Text += "<br /><br />";

						errLogin.Controls.Add(new LiteralControl("<br /><br />"));
						errLogin.Controls.Add(litError);
					}
				}
				else {
					LiteralControl litError = new LiteralControl();
					litError.Text = "<div class=\"successBox\">";
					litError.Text += "<span class=\"txtH3\">" + GetLocalResourceObject("successTitle").ToString() + "</span>";
					litError.Text += "<br /><br />";
					litError.Text += "<span class=\"txtNormal\">" + GetLocalResourceObject("successText").ToString() + "</span>";
					litError.Text += "<br /><br />";
					litError.Text += "<a class=\"normal\" href=\"login.aspx\">" + GetLocalResourceObject("LoginNow").ToString() + "</a>";
					litError.Text += "</div>";
					litError.Text += "<br /><br />";

					errLogin.Controls.Add(new LiteralControl("<br /><br />"));
					errLogin.Controls.Add(litError);
				}
			}
		}
	}

	/// <summary>
	/// btnRetrieve_Click
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnRetrieve_Click(object sender, EventArgs e) {
		if(this.txtEMail.Text.Trim() == "") {
			return;
		}

		if(this.txtUsername.Text.Trim() == "") {
			return;
		}

		CheckAnswer chka = ap.RetrieveNewPassword(this.txtEMail.Text, this.txtUsername.Text);

		if(chka == CheckAnswer.Successfull) {
			Response.Redirect("~/login.aspx?id=retrieve&message=99");
		}
		else {
			Response.Redirect("~/login.aspx?id=retrieve&message=12");
		}
	}
}
